﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace FAST
{
    public class Serializer
    {
        public Serializer() 
        { 
        }

        public void serializeObject(string filePath, List<FilesHandler> originalList)
        {

            if (File.Exists(filePath))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
            }

            Stream stream = File.Open(filePath, FileMode.Create);
            BinaryFormatter bFormat = new BinaryFormatter();
            bFormat.Serialize(stream, originalList);
            stream.Close();

            //Hide and Disallow user to access the storage file.
            File.SetAttributes(filePath, FileAttributes.Hidden);
        }

        public List<FilesHandler> deserializeObject(string filePath)
        {
            List<FilesHandler> originalList = null;

            if (File.Exists(filePath))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
            }

            Stream stream = File.Open(filePath, FileMode.Open);
            BinaryFormatter bFormat = new BinaryFormatter();
            originalList = (List<FilesHandler>)bFormat.Deserialize(stream);
            stream.Close();

            //Hide and Disallow user to access the storage file.
            File.SetAttributes(filePath, FileAttributes.Hidden);

            return originalList;
        }

        public void serializeSessList(string filePath, List<SessionItem> sessList)
        {

            if (File.Exists(filePath))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
            }

            Stream stream = File.Open(filePath, FileMode.Create);
            BinaryFormatter bFormat = new BinaryFormatter();
            bFormat.Serialize(stream, sessList);
            stream.Close();

            //Hide and Disallow user to access the storage file.
            File.SetAttributes(filePath, FileAttributes.Hidden);
        }

        public List<SessionItem> deserializeSessList(string filePath)
        {
            List<SessionItem> sessList = null;

            if (File.Exists(filePath))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
            }
            try
            {
                Stream stream = File.Open(filePath, FileMode.Open);
                BinaryFormatter bFormat = new BinaryFormatter();
                sessList = (List<SessionItem>)bFormat.Deserialize(stream);
                stream.Close();
            }
            catch (Exception)
            {

            }
            //Hide and Disallow user to access the storage file.
            File.SetAttributes(filePath, FileAttributes.Hidden);
            return sessList;
        }

        public void serializeFVList(string filePath, List<FileVersioningList> originalList)
        {

            if (File.Exists(filePath))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
            }
            try
            {
                Stream stream = File.Open(filePath, FileMode.Create);
                BinaryFormatter bFormat = new BinaryFormatter();
                bFormat.Serialize(stream, originalList);
                stream.Close();


                //Hide and Disallow user to access the storage file.
                File.SetAttributes(filePath, FileAttributes.Hidden);
            }
            catch (Exception) 
            { 
            }
        }

        public List<FileVersioningList> deserializeFVList(string filePath)
        {
            List<FileVersioningList> originalList = null;

            if (File.Exists(filePath))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
            }

            Stream stream = File.Open(filePath, FileMode.Open);
            BinaryFormatter bFormat = new BinaryFormatter();
            originalList = (List<FileVersioningList>)bFormat.Deserialize(stream);
            stream.Close();

            //Hide and Disallow user to access the storage file.
            File.SetAttributes(filePath, FileAttributes.Hidden);

            return originalList;
        }

    }
}
